//
//  JPSuspensionFunctionBaseContentView.m
//  JPSuspensionViewDemo
//
//  Created by Carpenter on 2018/2/12.
//  Copyright © 2018年 carpenter. All rights reserved.
//

#import "JPSuspensionFunctionBaseContentView.h"
#import <Masonry/Masonry.h>

@interface JPSuspensionFunctionBaseContentView()

/**
 删除按钮
 */
@property (nonatomic ,strong) UIButton * privateDeleteButton;

/**
 配置信息
 */
@property (nonatomic ,strong) JPSuspensionConfigure * privateConfigure;

@end

@implementation JPSuspensionFunctionBaseContentView

-(UIButton *)deleteButton{
    return self.privateDeleteButton;
}
-(JPSuspensionConfigure *)configure{
    return self.privateConfigure;
}

-(instancetype)initWithConfigure:(JPSuspensionConfigure *)configure{
    
    if (self = [super init]) {
        
        self.privateConfigure = configure ?: [JPSuspensionConfigure new];
        
        [self jp_setupView];
        
    }
    
    return self;
}
- (instancetype)init{

    if (self = [super init]) {
    
        self.privateConfigure = [[JPSuspensionConfigure alloc] init];
        
        [self jp_setupView];
        
    }
    return self;
}
-(void)jp_setupView{
    
    self.layer.cornerRadius = self.privateConfigure.clipRadius;
    
    self.layer.masksToBounds = true;
    
    self.backgroundColor = [UIColor whiteColor];
    
    [self jp_listeningKeyboard];
  
    
    //创建移除按钮
    self.privateDeleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.privateDeleteButton setImage:[self getJPSuspensionBundleImage:@"delete"]  forState:UIControlStateNormal];
    [self.privateDeleteButton addTarget:self action:@selector(privateDeleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.privateDeleteButton];
    [self.privateDeleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-5);
        make.top.mas_equalTo(5);
        make.width.height.mas_equalTo(30);
    }];
    self.privateDeleteButton.hidden = self.privateConfigure.hiddenDeleteButton;
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    //将删除按钮移到最外侧，防止被遮盖
    if (self.privateDeleteButton) [self bringSubviewToFront:self.privateDeleteButton];
}


//监听键盘
-(void)jp_listeningKeyboard{
    
    //防止悬浮视图上键盘弹起遮挡视图(依赖第三方IQKeyBoardManager）
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateViewFrame) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateViewFrame) name:UIKeyboardWillHideNotification object:nil];
    
}
-(void)updateViewFrame{
    
    CGRect rect = [UIApplication sharedApplication].delegate.window.rootViewController.view.frame;
    
    self.superview.frame = rect;
}
-(void)privateDeleteButtonClick:(UIButton *)sender{
    
    //将移除按钮点击状态传递到父视图
    if (self.didCancelButtonBlock) {
        self.didCancelButtonBlock();
    }
    
}
//获取JPSuspension.bundle下资源图片
-(UIImage*)getJPSuspensionBundleImage:(NSString*)name{
    
    NSBundle * bundle = [NSBundle bundleForClass:[JPSuspensionFunctionBaseContentView class]];
    NSURL * url = [bundle URLForResource:@"JPSuspension" withExtension:@"bundle"];
    bundle = [NSBundle bundleWithURL:url];
    
    int scale = (int)[UIScreen mainScreen].scale;
    
    NSString * imageName = [NSString stringWithFormat:@"%@@%dx",name,scale];
    
    NSString * path = [bundle pathForResource:imageName ofType:@"png"];
    
    UIImage * image = [UIImage imageWithContentsOfFile:path];
    
    return image;
}

@end
